package com.yuwenxin.servlet.filter;

import com.yuwenxin.bean.User;
import com.yuwenxin.dao.UserDao;
import com.yuwenxin.dao.impl.UserDaoImpl;
import com.yuwenxin.service.UserService;
import com.yuwenxin.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class AutoLoginFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("AutoLoginFilter initial...");
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        System.out.println("autoLoginFilter is doing...");
        UserService service = new UserServiceImpl();

//        System.out.println("request.getSession().getServletContext().getRealPath():" + request.getSession().getServletContext().getRealPath("/"));
//        System.out.println("request.getContextPath():" + request.getContextPath());

        Cookie[] cookies = request.getCookies();
        String autoLogin = null;
        for (int i=0; cookies!=null && i<cookies.length; i++){
            if ("autoLogin".equals(cookies[i].getName())){
                System.out.println("找到了autoLogin的cookie");
                autoLogin = cookies[i].getValue();
                System.out.println(autoLogin);
                break;
            }
        }

        if (autoLogin != null){
            String[] parts = autoLogin.split("-");
            String name = parts[0];
            String password = parts[1];
            System.out.println(name);
            System.out.println(password);

            User user;
            try {
                user = service.findUserByName(name);
                System.out.println(name + "密码：" +user.getPassword());

                if (!user.getPassword().equals(password)){
                    request.setAttribute("PSWDMes","密码错误，被修改");
                    request.getSession().removeAttribute("user");
                    Cookie cookie = new Cookie("autoLogin","msg");
                    cookie.setPath(request.getContextPath());
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                    request.getRequestDispatcher(request.getSession().getServletContext().
                            getRealPath("/client_jsp/login.jsp")).forward(request,response);  // 转发。可带参数传递。
                    // 使用绝对路径传递参数，避免出现转发错误的情况 --9.20
                }else {
                    user.setName(name);
                    user.setPassword(password);
                    service.userUpdate(user);
                    request.getSession().setAttribute("user",user);
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            System.out.println("autoLogin is null!!!");
        }

        filterChain.doFilter(request,servletResponse);
    }

    public void destroy() {
        System.out.println("AutoLoginFilter destroy...");
    }
}
